import plugin from "../../../lib/plugins/plugin.js";
import fs from "node:fs";
import { segment } from "oicq";
import path from "path"
import Show from "../model/show.js";
import puppeteer from "../../../lib/puppeteer/puppeteer.js";
import data from '../model/XiuxianData.js'
import { get_random_talent } from '../apps/xiuxian.js'

const _path = process.cwd() + "/plugins/xiuxian-emulator-plugin";
// 文件存放路径
const __PATH = {
    //用户数据
    player_path: path.join(_path, "/resources/data/xiuxian_player"),
    //装备
    equipment_path: path.join(_path, "/resources/data/xiuxian_equipment"),
    //纳戒
    najie_path: path.join(_path, "/resources/data/xiuxian_najie"),
    //源数据
    lib_path: path.join(_path, "/resources/data/item"),
}


export class showData extends plugin {
    constructor(e) {
        super({
            name: "修仙存档展示",
            dsc: "修仙存档展示",
            event: "message",
            priority: 500,
            rule: [
                {
                    reg: "^#(我的装备)$",
                    fnc: "show_equipment",
                },
                {
                    reg: "^#境界列表$",
                    fnc: "show_Level",
                },
                {
                    reg: "^#万宝楼$",
                    fnc: "show_valuables",
                }
            ]
        });

        this.path = __PATH.player_path;
    }


    async show_equipment(e) {
        let img = await get_equipment_img(e);
        e.reply(img);
        return;
    }


    async show_Level(e) {
        let img = await get_state_img(e);
        e.reply(img);
        return;
    }


    //万宝楼
    async show_valuables(e) {
        let img = await get_valuables_img(e);
        e.reply(img);
        return;
    }
}
//////////////////////////////////////////////////

/**
 * 返回该玩家的存档图片
 * @return image
 */
export async function get_player_img(e) {
    let usr_qq = e.user_id;
    let player = await data.getData("player", usr_qq);
    let equipment = await data.getData("equipment", usr_qq);
    let player_status = await getPlayerAction(usr_qq);
    let status = "空闲";
    if (player_status.time != null) {
        status = player_status.action + "(剩余时间:" + player_status.time + ")";
    }
    let lingshi = player.灵石;
    if (player.灵石 > 9999999999) {
        lingshi = 9999999999;//太多不美观
    }
    if (player.宣言 == null || player.宣言 == undefined) {
        player.宣言 = "这个人很懒什么都没写"
    }
    if (player.灵根 == null || player.灵根 == undefined) {
        player.灵根 = await get_random_talent();
        player.修炼效率提升 += player.灵根.eff;
    }
    await data.setData("player", usr_qq, player);

    //这几行千万别写进存档里面去!!!!!!
    let this_association = {};
    if (!isNotNull(player.宗门)) {
        this_association = {
            "宗门名称": "本人尚未加入宗门,还是一介散修",
            "职位": "无"
        };
    }else{
        this_association = player.宗门;
    }

    let player_data = {
        user_id: usr_qq,
        nickname: player.名号,
        linggen: player.灵根,
        declaration: player.宣言,
        exp: player.修为,
        level: player.境界,
        lingshi: lingshi,
        player_maxHP: player.血量上限,
        player_nowHP: player.当前血量,
        arms: equipment.武器,
        armor: equipment.护具,
        treasure: equipment.法宝,
        talent: parseInt(player.修炼效率提升 * 100),
        player_action: status,
        learned_gongfa: player.学习的功法,
        association: this_association
    }

    const data1 = await new Show(e).get_playerData(player_data);
    let img = await puppeteer.screenshot("player", {
        ...data1,
    });

    return img;
}

/**
 * 返回该玩家的装备图片
 * @return image
 */
export async function get_equipment_img(e) {
    let usr_qq = e.user_id;
    let player = await data.getData("player", usr_qq);
    let equipment = await data.getData("equipment", usr_qq);
    let player_data = {
        user_id: usr_qq,
        nickname: player.名号,
        arms: equipment.武器,
        armor: equipment.护具,
        treasure: equipment.法宝,
        player_atk: player.攻击,
        player_def: player.防御,
        player_bao: parseInt(player.暴击率 * 100),
        player_maxHP: player.血量上限,
        player_nowHP: player.当前血量

    }
    const data1 = await new Show(e).get_equipmnetData(player_data);
    let img = await puppeteer.screenshot("equipment", {
        ...data1,
    });

    return img;
}

/**
 * 返回该玩家的纳戒图片
 * @return image
 */
export async function get_najie_img(e) {
    let usr_qq = e.user_id;
    let player = await data.getData("player", usr_qq);
    let najie = await data.getData("najie", usr_qq);

    let player_data = {
        user_id: usr_qq,
        nickname: player.名号,
        najie_lv: najie.等级,
        player_maxHP: player.血量上限,
        player_nowHP: player.当前血量,
        najie_maxlingshi: najie.灵石上限,
        najie_lingshi: najie.灵石,
        najie_equipment: najie.装备,
        najie_danyao: najie.丹药,
        najie_daoju: najie.道具,
        najie_gongfa: najie.功法

    }
    const data1 = await new Show(e).get_najieData(player_data);
    let img = await puppeteer.screenshot("najie", {
        ...data1,
    });

    return img;

}


/**
 * 返回境界列表图片
 * @return image
 */

export async function get_state_img(e) {
    let usr_qq = e.user_id;
    let player = await data.getData("player", usr_qq);
    //let Player_level_id = data.Level_list.find(item => item.level == player.境界).level_id;
    // let state_human = [];
    // let state_god = [];
    // if (Player_level_id < 34) {
    //     for (var i = 0; i < Player_level_id; i++) {
    //         state_human.push(data.Level_list[i]);
    //     }
    //     for (var i = Player_level_id; i < 33; i++) {
    //         data.Level_list[i].exp = "未知";
    //         state_human.push(data.Level_list[i]);
    //     }
    //     for (var i = 33; i < 38; i++) {
    //         data.Level_list[i].exp = "未知";
    //         state_god.push(data.Level_list[i]);
    //     }
    // }
    // else if (Player_level_id < 38) {
    //     for (var i = 0; i < 33; i++) {
    //         state_human.push(data.Level_list[i]);
    //     }
    //     for (var i = 33; i < Player_level_id; i++) {
    //         state_god.push(data.Level_list[i]);
    //     }
    //     for (var i = Player_level_id; i < 38; i++) {
    //         data.Level_list[i].exp = "未知";
    //         state_human.push(data.Level_list[i]);
    //     }
    // }
    // else if (Player_level_id = 38) {
    //     for (var i = 0; i < 34; i++) {
    //         state_human.push(data.Level_list[i]);
    //     }
    //     for (var i = 34; i < 37; i++) {
    //         state_god.push(data.Level_list[i]);
    //     }
    //     data.Level_list[37].exp = "未知";
    //     state_human.push(data.Level_list[37]);
    // }


    let state_data = {
        user_id: usr_qq,
        nickname: player.名号,
        player_maxHP: player.血量上限,
        player_nowHP: player.当前血量,
        exp: player.修为,
        level: player.境界,
        //state_human_list: state_human,
        //state_god_list: state_god
    }
    const data1 = await new Show(e).get_stateData(state_data);
    let img = await puppeteer.screenshot("state", {
        ...data1,
    });
    return img;

}

/**
 * 返回万宝楼
 * @return image
 */

export async function get_valuables_img(e) {
    let usr_qq = e.user_id;
    let valuables_data = {
        user_id: usr_qq
    }
    const data1 = await new Show(e).get_valuablesData(valuables_data);
    let img = await puppeteer.screenshot("valuables", {
        ...data1,
    });
    return img;

}


// /**
//  * 返回帮图片
//  * @return image
//  */

// export async function get_help_img(e) {
//     let usr_qq = e.user_id;
//     let help_data = {
//         user_id: usr_qq
//     }
//     const data1 = await new Show(e).get_helpData(help_data);
//     let img = await puppeteer.screenshot("help", {
//         ...data1,
//     });
//     return img;
// }


async function getPlayerAction(usr_qq) {
    //查询redis中的人物动作
    let arr = {};
    let action = await redis.get("xiuxian:player:" + usr_qq + ":action");
    action = JSON.parse(action);
    if (action != null) {
        //人物有动作查询动作结束时间
        let action_end_time = action.end_time;
        let now_time = new Date().getTime();
        if (now_time <= action_end_time) {
            let m = parseInt((action_end_time - now_time) / 1000 / 60);
            let s = parseInt(((action_end_time - now_time) - m * 60 * 1000) / 1000);
            arr.action = action.action;//当期那动作
            arr.time = m + "分" + s + "秒";//剩余时间
            return arr;
        }
    }
    arr.action = "空闲";
    return arr;
}

/**
 * 判断对象是否不为undefined且不为null
 * @param obj 对象
 * @returns obj==null/undefined,return false,other return true
 */
function isNotNull(obj) {
    if (obj == undefined || obj == null)
        return false;
    return true;
}